﻿using System;
using System.Web.UI.WebControls;

namespace Carbon.Web.Forms.jQueryForms
{
    public class RadioButtonListEditor : RadioButtonList, IEditor
    {
		#region Fields (2)
		private IRadioButtonListItemCollection m_Items = default(IRadioButtonListItemCollection);

		private ListEditor m_Parent = default(ListEditor);
		#endregion

		#region Properties (5)
		public bool IsNull
        {
            get
            {
                IRadioButtonListItemCollection collection = (IRadioButtonListItemCollection)Value;

                return collection.SelectedIndex == null;
            }
        }
		public new IRadioButtonListItemCollection Items
        {
            get
            {
                return m_Items;
            }
            private set
            {
                m_Items = value;
                base.Items.Clear();
                int ordinal = 0;
                foreach (IListEditorItem item in Items)
                {
                    base.Items.Add(new ListItem(Items.GetDisplayName(item.Data), ConvertInvariant.ToString(ordinal++)) { Selected = false });
                }

                this.SelectedIndex = Items.SelectedIndex ?? -1;
            }
        }
		public new ListEditor Parent
        {
            get
            {
                return m_Parent;
            }
        }
		public bool ReadOnly
        {
            get
            {
                return !this.Enabled;
            }
            set
            {
                this.Enabled = !value;
            }
        }
		public object Value
        {
            get
            {
                if (m_Items.SelectedIndex >= 0)
                {
                    m_Items.SelectedIndex = m_Items.SelectedIndex;
                }
                else
                {
                    m_Items.SelectedIndex = null;
                }
                return Items;
            }
            set
            {
                Items = (IRadioButtonListItemCollection)value;
            }
        }
		#endregion

		#region Constructors (1)
		public RadioButtonListEditor(IRadioButtonListItemCollection items, ListEditor parent)
            : base()
        {
            if (parent == null) throw new NullReferenceException("parent");
            if (items == null) throw new ArgumentNullException("items");

            this.Items = items;
            this.m_Parent = parent;
        }
		#endregion
    }
}
